<template>
  <div class="app-container">
    <el-form :model="queryParams" ref="queryForm" size="small" :inline="true" v-show="showSearch" label-width="80px">
      <el-form-item label="物品名称" prop="inventoryName">
        <el-input
          v-model="queryParams.inventoryName"
          placeholder="请输入物品名称"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="规格" prop="inventorySpecifications">
        <el-input
          v-model="queryParams.inventorySpecifications"
          placeholder="请输入规格"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="供应商" prop="supply">
        <el-input
          v-model="queryParams.supply"
          placeholder="请输入供应商"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="wbs编码" prop="wbs">
        <el-input
          v-model="queryParams.wbs"
          placeholder="请输入wbs编码"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="库位" prop="location">
        <el-input
          v-model="queryParams.location"
          placeholder="请输入库位"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item>
        <el-button type="primary" icon="el-icon-search" size="mini" @click="handleQuery">搜索</el-button>
        <el-button icon="el-icon-refresh" size="mini" @click="resetQuery">重置</el-button>
      </el-form-item>
    </el-form>

    <el-row :gutter="10" class="mb8">
      <el-col :span="1.5">
        <el-button
          type="primary"
          plain
          icon="el-icon-plus"
          size="mini"
          @click="handleAdd"
          v-hasPermi="['purchase:ryDepotDetail:add']"
        >新增</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="success"
          plain
          icon="el-icon-edit"
          size="mini"
          :disabled="single"
          @click="handleUpdate"
          v-hasPermi="['purchase:ryDepotDetail:edit']"
        >修改</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="danger"
          plain
          icon="el-icon-delete"
          size="mini"
          :disabled="multiple"
          @click="handleDelete"
          v-hasPermi="['purchase:ryDepotDetail:remove']"
        >删除</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="warning"
          plain
          icon="el-icon-download"
          size="mini"
          @click="handleExport"
          v-hasPermi="['purchase:ryDepotDetail:export']"
        >导出</el-button>
      </el-col>
      <right-toolbar :showSearch.sync="showSearch" @queryTable="getList"></right-toolbar>
    </el-row>

    <el-table v-loading="loading" :data="ryDepotDetailList" @selection-change="handleSelectionChange">
      <el-table-column type="selection" width="55" align="center" />
      <el-table-column label="主键" align="center" prop="id" />
      <el-table-column label="物品id" align="center" prop="inventoryId" />
      <el-table-column label="物品名称" align="center" prop="inventoryName" />
      <el-table-column label="规格" align="center" prop="inventorySpecifications" />
      <el-table-column label="供应商id" align="center" prop="supplyId" />
      <el-table-column label="供应商" align="center" prop="supply" />
      <el-table-column label="wbs编码" align="center" prop="wbs" />
      <el-table-column label="待采购数量" align="center" prop="advancePurchaseNum" />
      <el-table-column label="总库可用数量" align="center" prop="usableNum" />
      <el-table-column label="实际采购数量" align="center" prop="actualPurchaseNum" />
      <el-table-column label="发货状态" align="center" prop="shippingStatus">
        <template slot-scope="scope">
          <dict-tag :options="dict.type.shipping_status" :value="scope.row.shippingStatus"/>
        </template>
      </el-table-column>
      <el-table-column label="采购人" align="center" prop="purchaseMan" />
      <el-table-column label="合格数量" align="center" prop="qualifiedNum" />
      <el-table-column label="不合格数量" align="center" prop="unqualifiedNum" />
      <el-table-column label="不合格原因" align="center" prop="unqualifiedReason" />
      <el-table-column label="退回状态" align="center" prop="returnStatus">
        <template slot-scope="scope">
          <dict-tag :options="dict.type.return_status" :value="scope.row.returnStatus"/>
        </template>
      </el-table-column>
      <el-table-column label="重发状态" align="center" prop="retransmissionStatus">
        <template slot-scope="scope">
          <dict-tag :options="dict.type.retransmission_status" :value="scope.row.retransmissionStatus"/>
        </template>
      </el-table-column>
      <el-table-column label="重发数量" align="center" prop="retransmissionNum" />
      <el-table-column label="质检员" align="center" prop="qualityInspector" />
      <el-table-column label="质检时间" align="center" prop="qualityTime" width="180">
        <template slot-scope="scope">
          <span>{{ parseTime(scope.row.qualityTime, '{y}-{m}-{d}') }}</span>
        </template>
      </el-table-column>
      <el-table-column label="入库数量" align="center" prop="inNum" />
      <el-table-column label="入库仓库id" align="center" prop="inDepotId" />
      <el-table-column label="入库仓库" align="center" prop="inDepot" />
      <el-table-column label="库位" align="center" prop="location" />
      <el-table-column label="入库时间" align="center" prop="inTime" width="180">
        <template slot-scope="scope">
          <span>{{ parseTime(scope.row.inTime, '{y}-{m}-{d}') }}</span>
        </template>
      </el-table-column>
      <el-table-column label="入库操作人员" align="center" prop="inOperateMan" />
      <el-table-column label="出库数量" align="center" prop="outNum" />
      <el-table-column label="出库仓库id" align="center" prop="outDepotId" />
      <el-table-column label="出库仓库" align="center" prop="outDepot" />
      <el-table-column label="出库时间" align="center" prop="outTime" width="180">
        <template slot-scope="scope">
          <span>{{ parseTime(scope.row.outTime, '{y}-{m}-{d}') }}</span>
        </template>
      </el-table-column>
      <el-table-column label="出库操作人员" align="center" prop="outOperateMan" />
      <el-table-column label="领用人" align="center" prop="outForMan" />
      <el-table-column label="操作" align="center" class-name="small-padding fixed-width">
        <template slot-scope="scope">
          <el-button
            size="mini"
            type="text"
            icon="el-icon-edit"
            @click="handleUpdate(scope.row)"
            v-hasPermi="['purchase:ryDepotDetail:edit']"
          >修改</el-button>
          <el-button
            size="mini"
            type="text"
            icon="el-icon-delete"
            @click="handleDelete(scope.row)"
            v-hasPermi="['purchase:ryDepotDetail:remove']"
          >删除</el-button>
        </template>
      </el-table-column>
    </el-table>

    <pagination
      v-show="total>0"
      :total="total"
      :page.sync="queryParams.pageNum"
      :limit.sync="queryParams.pageSize"
      @pagination="getList"
    />

    <!-- 添加或修改采供总清单明细对话框 -->
    <el-dialog :title="title" :visible.sync="open" width="700px" append-to-body>
      <el-form ref="form" :model="form" :rules="rules" label-width="100px">
        <el-form-item label="物品id" prop="inventoryId">
          <el-input v-model="form.inventoryId" placeholder="请输入物品id" />
        </el-form-item>
        <el-form-item label="物品名称" prop="inventoryName">
          <el-input v-model="form.inventoryName" placeholder="请输入物品名称" />
        </el-form-item>
        <el-form-item label="规格" prop="inventorySpecifications">
          <el-input v-model="form.inventorySpecifications" placeholder="请输入规格" />
        </el-form-item>
        <el-form-item label="供应商id" prop="supplyId">
          <el-input v-model="form.supplyId" placeholder="请输入供应商id" />
        </el-form-item>
        <el-form-item label="供应商" prop="supply">
          <el-input v-model="form.supply" placeholder="请输入供应商" />
        </el-form-item>
        <el-form-item label="wbs编码" prop="wbs">
          <el-input v-model="form.wbs" placeholder="请输入wbs编码" />
        </el-form-item>
        <el-form-item label="待采购数量" prop="advancePurchaseNum">
          <el-input v-model="form.advancePurchaseNum" placeholder="请输入待采购数量" />
        </el-form-item>
        <el-form-item label="总库可用数量" prop="usableNum">
          <el-input v-model="form.usableNum" placeholder="请输入总库可用数量" />
        </el-form-item>
        <el-form-item label="实际采购数量" prop="actualPurchaseNum">
          <el-input v-model="form.actualPurchaseNum" placeholder="请输入实际采购数量" />
        </el-form-item>
        <el-form-item label="发货状态" prop="shippingStatus">
          <el-select v-model="form.shippingStatus" placeholder="请选择发货状态">
            <el-option
              v-for="dict in dict.type.shipping_status"
              :key="dict.value"
              :label="dict.label"
:value="dict.value"
            ></el-option>
          </el-select>
        </el-form-item>
        <el-form-item label="采购人" prop="purchaseMan">
          <el-input v-model="form.purchaseMan" placeholder="请输入采购人" />
        </el-form-item>
        <el-form-item label="合格数量" prop="qualifiedNum">
          <el-input v-model="form.qualifiedNum" placeholder="请输入合格数量" />
        </el-form-item>
        <el-form-item label="不合格数量" prop="unqualifiedNum">
          <el-input v-model="form.unqualifiedNum" placeholder="请输入不合格数量" />
        </el-form-item>
        <el-form-item label="不合格原因" prop="unqualifiedReason">
          <el-input v-model="form.unqualifiedReason" placeholder="请输入不合格原因" />
        </el-form-item>
        <el-form-item label="退回状态">
          <el-radio-group v-model="form.returnStatus">
            <el-radio
              v-for="dict in dict.type.return_status"
              :key="dict.value"
:label="dict.value"
            >{{dict.label}}</el-radio>
          </el-radio-group>
        </el-form-item>
        <el-form-item label="重发状态" prop="retransmissionStatus">
          <el-select v-model="form.retransmissionStatus" placeholder="请选择重发状态">
            <el-option
              v-for="dict in dict.type.retransmission_status"
              :key="dict.value"
              :label="dict.label"
:value="dict.value"
            ></el-option>
          </el-select>
        </el-form-item>
        <el-form-item label="重发数量" prop="retransmissionNum">
          <el-input v-model="form.retransmissionNum" placeholder="请输入重发数量" />
        </el-form-item>
        <el-form-item label="质检员" prop="qualityInspector">
          <el-input v-model="form.qualityInspector" placeholder="请输入质检员" />
        </el-form-item>
        <el-form-item label="质检时间" prop="qualityTime">
          <el-date-picker clearable
            v-model="form.qualityTime"
            type="date"
            value-format="yyyy-MM-dd"
            placeholder="请选择质检时间">
          </el-date-picker>
        </el-form-item>
        <el-form-item label="入库数量" prop="inNum">
          <el-input v-model="form.inNum" placeholder="请输入入库数量" />
        </el-form-item>
        <el-form-item label="入库仓库id" prop="inDepotId">
          <el-input v-model="form.inDepotId" placeholder="请输入入库仓库id" />
        </el-form-item>
        <el-form-item label="入库仓库" prop="inDepot">
          <el-input v-model="form.inDepot" placeholder="请输入入库仓库" />
        </el-form-item>
        <el-form-item label="库位" prop="location">
          <el-input v-model="form.location" placeholder="请输入库位" />
        </el-form-item>
        <el-form-item label="入库时间" prop="inTime">
          <el-date-picker clearable
            v-model="form.inTime"
            type="date"
            value-format="yyyy-MM-dd"
            placeholder="请选择入库时间">
          </el-date-picker>
        </el-form-item>
        <el-form-item label="入库操作人员" prop="inOperateMan">
          <el-input v-model="form.inOperateMan" placeholder="请输入入库操作人员" />
        </el-form-item>
        <el-form-item label="出库数量" prop="outNum">
          <el-input v-model="form.outNum" placeholder="请输入出库数量" />
        </el-form-item>
        <el-form-item label="出库仓库id" prop="outDepotId">
          <el-input v-model="form.outDepotId" placeholder="请输入出库仓库id" />
        </el-form-item>
        <el-form-item label="出库仓库" prop="outDepot">
          <el-input v-model="form.outDepot" placeholder="请输入出库仓库" />
        </el-form-item>
        <el-form-item label="出库时间" prop="outTime">
          <el-date-picker clearable
            v-model="form.outTime"
            type="date"
            value-format="yyyy-MM-dd"
            placeholder="请选择出库时间">
          </el-date-picker>
        </el-form-item>
        <el-form-item label="出库操作人员" prop="outOperateMan">
          <el-input v-model="form.outOperateMan" placeholder="请输入出库操作人员" />
        </el-form-item>
        <el-form-item label="领用人" prop="outForMan">
          <el-input v-model="form.outForMan" placeholder="请输入领用人" />
        </el-form-item>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button type="primary" @click="submitForm">确 定</el-button>
        <el-button @click="cancel">取 消</el-button>
      </div>
    </el-dialog>
  </div>
</template>

<script>
import { listRyDepotDetail, getRyDepotDetail, delRyDepotDetail, addRyDepotDetail, updateRyDepotDetail } from "@/api/purchase/ryDepotDetail";

export default {
  name: "RyDepotDetail",
  dicts: ['return_status', 'shipping_status', 'retransmission_status'],
  data() {
    return {
      // 遮罩层
      loading: true,
      // 选中数组
      ids: [],
      // 非单个禁用
      single: true,
      // 非多个禁用
      multiple: true,
      // 显示搜索条件
      showSearch: true,
      // 总条数
      total: 0,
      // 采供总清单明细表格数据
      ryDepotDetailList: [],
      // 弹出层标题
      title: "",
      // 是否显示弹出层
      open: false,
      // 查询参数
      queryParams: {
        pageNum: 1,
        pageSize: 10,
        inventoryName: null,
        inventorySpecifications: null,
        supply: null,
        wbs: null,
        location: null,
      },
      // 表单参数
      form: {},
      // 表单校验
      rules: {
      }
    };
  },
  created() {
    this.getList();
  },
  methods: {
    /** 查询采供总清单明细列表 */
    getList() {
      this.loading = true;
      listRyDepotDetail(this.queryParams).then(response => {
        this.ryDepotDetailList = response.rows;
        this.total = response.total;
        this.loading = false;
      });
    },
    // 取消按钮
    cancel() {
      this.open = false;
      this.reset();
    },
    // 表单重置
    reset() {
      this.form = {
        id: null,
        inventoryId: null,
        inventoryName: null,
        inventorySpecifications: null,
        supplyId: null,
        supply: null,
        wbs: null,
        advancePurchaseNum: null,
        usableNum: null,
        actualPurchaseNum: null,
        shippingStatus: null,
        purchaseMan: null,
        qualifiedNum: null,
        unqualifiedNum: null,
        unqualifiedReason: null,
        returnStatus: "0",
        retransmissionStatus: null,
        retransmissionNum: null,
        qualityInspector: null,
        qualityTime: null,
        inNum: null,
        inDepotId: null,
        inDepot: null,
        location: null,
        inTime: null,
        inOperateMan: null,
        outNum: null,
        outDepotId: null,
        outDepot: null,
        outTime: null,
        outOperateMan: null,
        outForMan: null
      };
      this.resetForm("form");
    },
    /** 搜索按钮操作 */
    handleQuery() {
      this.queryParams.pageNum = 1;
      this.getList();
    },
    /** 重置按钮操作 */
    resetQuery() {
      this.resetForm("queryForm");
      this.handleQuery();
    },
    // 多选框选中数据
    handleSelectionChange(selection) {
      this.ids = selection.map(item => item.id)
      this.single = selection.length!==1
      this.multiple = !selection.length
    },
    /** 新增按钮操作 */
    handleAdd() {
      this.reset();
      this.open = true;
      this.title = "添加采供总清单明细";
    },
    /** 修改按钮操作 */
    handleUpdate(row) {
      this.reset();
      const id = row.id || this.ids
      getRyDepotDetail(id).then(response => {
        this.form = response.data;
        this.open = true;
        this.title = "修改采供总清单明细";
      });
    },
    /** 提交按钮 */
    submitForm() {
      this.$refs["form"].validate(valid => {
        if (valid) {
          if (this.form.id != null) {
            updateRyDepotDetail(this.form).then(response => {
              this.$modal.msgSuccess("修改成功");
              this.open = false;
              this.getList();
            });
          } else {
            addRyDepotDetail(this.form).then(response => {
              this.$modal.msgSuccess("新增成功");
              this.open = false;
              this.getList();
            });
          }
        }
      });
    },
    /** 删除按钮操作 */
    handleDelete(row) {
      const ids = row.id || this.ids;
      this.$modal.confirm('是否确认删除采供总清单明细编号为"' + ids + '"的数据项？').then(function() {
        return delRyDepotDetail(ids);
      }).then(() => {
        this.getList();
        this.$modal.msgSuccess("删除成功");
      }).catch(() => {});
    },
    /** 导出按钮操作 */
    handleExport() {
      this.download('purchase/ryDepotDetail/export', {
        ...this.queryParams
      }, `ryDepotDetail_${new Date().getTime()}.xlsx`)
    }
  }
};
</script>
